package exttable.formgenerator;

import dbutils.DbUtils;
import exttable.SGTTestParametrs;
import exttable.dbclasses.Group;
import exttable.dbclasses.Phase;
import utils.Localisation;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ADMIN
 * Date: 13.12.2010
 * Time: 14:17:10
 * To change this template use File | Settings | File Templates.
 */
public class FilterSelector extends JFrame {
    private List<Phase> phaseList = Phase.getPhaseList();
    private JButton nextButton;
    private static List<Object> phaseToReturnList = new ArrayList<Object>();
    private static List<Object> groupToReturnList;
    private static List<Object> probationerToReturnList;
    private static String tableName;

    private JPanel mainPanel;
    private SpringLayout springLayout;

    private JLabel labelPhase;
    private JScrollPane jScrollPaneForPhaseJList;
    private JList phaseJList;
    private JCheckBox selectAllPhase;

    private JLabel labelGroup;
    private JScrollPane jScrollPaneForGroupsJList;
    private JList groupsJList;
    private JCheckBox selectAllGroups;

    private JLabel labelProbationer;
    private JScrollPane jScrollPaneForProbationerJList;
    private JList probationerJList;
    private JCheckBox selectAllProbationers;

    public static List<Object> getPhaseToReturnList() {
        return phaseToReturnList;
    }

    public static List<Object> getGroupToReturnList() {
        return groupToReturnList;
    }

    public static List<Object> getProbationerToReturnList() {
        return probationerToReturnList;
    }

    public static String getTableName() {
        return tableName;
    }

    public FilterSelector(String tableName) {
        this.tableName = tableName;
        mainPanel = new JPanel();
        labelPhase = new JLabel(Localisation.getMessage("form.phase"));
        phaseJList = new JList(phaseList.toArray());
//        phaseJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//        phaseJList.addListSelectionListener(new ListSelectionListener() {
//            @Override
//            public void valueChanged(ListSelectionEvent e) {
//                if (!phaseJList.isSelectionEmpty()) {
////                    groupsJList.setListData(((Phase) phaseJList.getSelectedValue()).getGroupList().toArray());
//                    List<Object> tempGroupList = new ArrayList<Object>();
//                    for (Object phase : phaseJList.getSelectedValuesList()) {
//                        tempGroupList.addAll(((Phase) phase).getGroupList());
//                    }
//                    groupsJList.setListData(tempGroupList.toArray());
//                }
//
//            }
//        });
        jScrollPaneForPhaseJList = new JScrollPane(phaseJList);
        selectAllPhase = new JCheckBox(Localisation.getMessage("label.selectAll"));
//        selectAllPhase.addActionListener(new ActionListener() {
//
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                if (selectAllPhase.isSelected()) {
//                    StaticMethods.selectAllIndicesInJList(phaseJList);
//                } else {
//                    phaseJList.clearSelection();
//                    groupsJList.setListData(new Array[]{});
//                }
//            }
//        });

//        selectAllGroups = new JCheckBox(Localisation.getMessage("label.selectAll"));
//        selectAllGroups.addActionListener(new ActionListener() {
//
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                if (selectAllGroups.isSelected()) {
//                    StaticMethods.selectAllIndicesInJList(groupsJList);
//                } else {
//                    groupsJList.clearSelection();
//                }
//            }
//        });

        nextButton = new JButton(Localisation.getMessage("form.continue"));
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (MessageFactory.isEmptyField(mainPanel.getComponents())) {
                    return;
                }

                phaseToReturnList.clear();
                phaseToReturnList.addAll(phaseJList.getSelectedValuesList());
                if (SGTTestParametrs.getInstance().getUserAccessLevel() < 4) {
                    probationerToReturnList.clear();
                    probationerToReturnList.addAll(probationerJList.getSelectedValuesList());
                } else {
                    groupToReturnList.clear();
                    groupToReturnList.addAll(groupsJList.getSelectedValuesList());
                }
                dispose();
                new ViewGenerator(FilterSelector.getTableName());
            }
        });
        mainPanel.add(labelPhase);
        mainPanel.add(jScrollPaneForPhaseJList);
        mainPanel.add(selectAllPhase);
        mainPanel.add(nextButton);
        add(mainPanel);
        organizeLayout();
        setTitle(Localisation.getMessage("title.filterByPhaseAndGroup"));
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(800, 500);
        setVisible(true);
    }

    private void createGroupGui() {
        groupToReturnList = new ArrayList<Object>();
        labelGroup = new JLabel(Localisation.getMessage("menu.group"));
        groupsJList = new JList();
        phaseJList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!phaseJList.isSelectionEmpty()) {
                    List<Object> tempGroupList = new ArrayList<Object>();
                    for (Object phase : phaseJList.getSelectedValuesList()) {
                        tempGroupList.addAll(((Phase) phase).getGroupList());
                    }
                    groupsJList.setListData(tempGroupList.toArray());
                }

            }
        });
        jScrollPaneForGroupsJList = new JScrollPane(groupsJList);
        selectAllPhase.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (selectAllPhase.isSelected()) {
                    StaticMethods.selectAllIndicesInJList(phaseJList);
                } else {
                    phaseJList.clearSelection();
                    groupsJList.setListData(new Array[]{});
                }
            }
        });

        selectAllGroups = new JCheckBox(Localisation.getMessage("label.selectAll"));
        selectAllGroups.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (selectAllGroups.isSelected()) {
                    StaticMethods.selectAllIndicesInJList(groupsJList);
                } else {
                    groupsJList.clearSelection();
                }
            }
        });

        mainPanel.add(labelGroup);
        mainPanel.add(jScrollPaneForGroupsJList);
        mainPanel.add(selectAllGroups);

        springLayout.putConstraint(SpringLayout.WEST, labelGroup, 100, SpringLayout.EAST, jScrollPaneForPhaseJList);
        springLayout.putConstraint(SpringLayout.NORTH, labelGroup, 0, SpringLayout.NORTH, labelPhase);

        springLayout.putConstraint(SpringLayout.WEST, jScrollPaneForGroupsJList, 25, SpringLayout.EAST, jScrollPaneForPhaseJList);
        springLayout.putConstraint(SpringLayout.NORTH, jScrollPaneForGroupsJList, 0, SpringLayout.NORTH, jScrollPaneForPhaseJList);

        springLayout.putConstraint(SpringLayout.WEST, selectAllGroups, 100, SpringLayout.WEST, jScrollPaneForGroupsJList);
        springLayout.putConstraint(SpringLayout.NORTH, selectAllGroups, 0, SpringLayout.NORTH, selectAllPhase);
    }

    private void createProbationerGui() {
        probationerToReturnList = new ArrayList<Object>();
        labelProbationer = new JLabel(Localisation.getMessage("menu.probationer"));
        probationerJList = new JList();
        phaseJList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!phaseJList.isSelectionEmpty()) {
                    List<Object> tempProbationerList = new ArrayList<Object>();
                    for (Object phase : phaseJList.getSelectedValuesList()) {
                        tempProbationerList.addAll(Phase.getProbationerListByPhase(((Phase) phase).getPhaseId()));
                    }
                    Collections.sort(tempProbationerList, ComparatorFactory.getProbationerCodeComparator());
                    probationerJList.setListData(tempProbationerList.toArray());
                }

            }
        });
        jScrollPaneForProbationerJList = new JScrollPane(probationerJList);
        selectAllPhase.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (selectAllPhase.isSelected()) {
                    StaticMethods.selectAllIndicesInJList(phaseJList);
                } else {
                    phaseJList.clearSelection();
                    probationerJList.setListData(new Array[]{});
                }
            }
        });

        selectAllProbationers = new JCheckBox(Localisation.getMessage("label.selectAll"));
        selectAllProbationers.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (selectAllProbationers.isSelected()) {
                    StaticMethods.selectAllIndicesInJList(probationerJList);
                } else {
                    probationerJList.clearSelection();
                }
            }
        });

        mainPanel.add(labelProbationer);
        mainPanel.add(jScrollPaneForProbationerJList);
        mainPanel.add(selectAllProbationers);

        springLayout.putConstraint(SpringLayout.WEST, labelProbationer, 100, SpringLayout.EAST, jScrollPaneForPhaseJList);
        springLayout.putConstraint(SpringLayout.NORTH, labelProbationer, 0, SpringLayout.NORTH, labelPhase);

        springLayout.putConstraint(SpringLayout.WEST, jScrollPaneForProbationerJList, 25, SpringLayout.EAST, jScrollPaneForPhaseJList);
        springLayout.putConstraint(SpringLayout.NORTH, jScrollPaneForProbationerJList, 0, SpringLayout.NORTH, jScrollPaneForPhaseJList);

        springLayout.putConstraint(SpringLayout.WEST, selectAllProbationers, 100, SpringLayout.WEST, jScrollPaneForProbationerJList);
        springLayout.putConstraint(SpringLayout.NORTH, selectAllProbationers, 0, SpringLayout.NORTH, selectAllPhase);
    }

    private void organizeLayout() {
        springLayout = new SpringLayout();
        mainPanel.setLayout(springLayout);

        if (SGTTestParametrs.getInstance().getUserAccessLevel() < 4) {
            createProbationerGui();
        } else {
            createGroupGui();
        }

        springLayout.putConstraint(SpringLayout.WEST, labelPhase, 100, SpringLayout.WEST, mainPanel);
        springLayout.putConstraint(SpringLayout.NORTH, labelPhase, 20, SpringLayout.WEST, mainPanel);

        springLayout.putConstraint(SpringLayout.WEST, jScrollPaneForPhaseJList, 10, SpringLayout.WEST, mainPanel);
        springLayout.putConstraint(SpringLayout.NORTH, jScrollPaneForPhaseJList, 10, SpringLayout.SOUTH, labelPhase);

        springLayout.putConstraint(SpringLayout.WEST, selectAllPhase, 100, SpringLayout.WEST, jScrollPaneForPhaseJList);
        springLayout.putConstraint(SpringLayout.NORTH, selectAllPhase, 10, SpringLayout.SOUTH, jScrollPaneForPhaseJList);

        springLayout.putConstraint(SpringLayout.WEST, nextButton, 100, SpringLayout.WEST, jScrollPaneForPhaseJList);
        springLayout.putConstraint(SpringLayout.NORTH, nextButton, 30, SpringLayout.NORTH, selectAllPhase);
    }
}
